package com.wang.experiment.experiment3.horseracing;

import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * @author wr
 * @date 2021/10/27 18:46
 */
public class Horse extends Thread {
    private final JProgressBar progress;
    private final JLabel rankLabel;
    private final Rank rank;
    private final Random random = new Random(System.currentTimeMillis());

    public Horse(JProgressBar progress, JLabel rankLabel, Rank rank) {
        this.progress = progress;
        this.rankLabel = rankLabel;
        this.rank = rank;
    }


    @Override
    public void run() {
        try {
            while (progress.getValue() != progress.getMaximum()) {
                sleep(1);
                progress.setValue(progress.getValue() + 1);
                //运行中再次随机设置优先级
                setPriority(random.nextInt(9) + 1);
            }
            rankLabel.setText(rank.getRank() + "");
        }catch (Exception e){
            //ignore
        }
    }
}
